package top.fengleifeng.question;

/**
 * @author : fengelfeing
 * @Package : top.fengleifeng.question
 * @Description:
 * @Date: 2020年10月30日2:09 下午
 */
public class Num463岛屿的周长 {

    class Solution {
        public int islandPerimeter(int[][] grid) {
            if (grid.length <= 0 || grid[0].length <= 0) {
                return 0;
            }
            boolean[][] checked = new boolean[grid.length][grid[0].length];
            int count = 0;
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[i].length; j++) {
                    if (grid[i][j] == 1 && !checked[i][j]) {
                        checked[i][j] = true;
                        int now = 4;
                        //上下左右
                        if (j - 1 >= 0 && grid[i][j - 1] == 1) {
                            now--;
                        }
                        if (j + 1 < grid[i].length && grid[i][j + 1] == 1) {
                            now--;
                        }
                        if (i - 1 >= 0 && grid[i - 1][j] == 1) {
                            now--;
                        }
                        if (i + 1 < grid.length && grid[i + 1][j] == 1) {
                            now--;
                        }
                        count = count + now;
                    }
                }
            }
            return count;
        }
    }

    public static void main(String[] args) {
//        [[0,1,0,0],
//         [1,1,1,0],
//         [0,1,0,0],
//         [1,1,0,0]]
        int[][] ints = {{0,1,0,0},{1,1,1,0},{0,1,0,0},{1,1,0,0}};
        int i = new Num463岛屿的周长().new Solution().islandPerimeter(ints);
        System.out.println(i);
    }
}
